<template>
  <el-row
    type="flex"
    justify="space-between"
    align="middle"
    style="width: 100%; height: 40px"
  >
    <!-- 行组件 -->
    <!-- 左 -->
    <el-col :span="12">
      <span>{{ treeNode.name }} </span>
    </el-col>

    <!-- 右 -->
    <el-col :span="4">
      <el-row type="flex" justify="end">
        <el-col>{{ treeNode.manager }}</el-col>
        <el-col>
          <!-- 下拉菜单 element -->
          <el-dropdown @command="operateDepts">
            <span> 操作<i class="el-icon-arrow-down" /> </span>
            <!-- 下拉菜单 -->
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="add">添加子部门</el-dropdown-item>
              <template v-if="!isRoot">
                <el-dropdown-item command="edit">编辑子部门</el-dropdown-item>
                <el-dropdown-item command="del"> 删除子部门</el-dropdown-item>
              </template>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import { removeDepartments } from '@/api/departments'
export default {
  props: {
    treeNode: {
      type: Object,
      required: true // 此值不传必报错
    },
    isRoot: {
      type: Boolean,
      default: false // 默认值
    }
  },

  methods: {
    // 进行操作 - 判断各种操作
    operateDepts(type) {
      if (type === 'add') {
        // 添加
        // 告诉父组件召唤弹出层, 并且告诉父组件要在那个部门下添加子部门
        this.$emit('addDepts', this.treeNode)
      } else if (type === 'edit') {
        // 编辑
        // 通知父组件召唤弹出层
        this.$emit('editDepts', this.treeNode)
      } else {
        // 删除
        // 提示
        this.$confirm('确定要删除该部门吗? ')
          .then(() => {
            //   调用删除接口
            return removeDepartments(this.treeNode.id)
          })
          .then(() => {
            //   删除成功
            this.$message.success('删除部门成功! ')
            // 重新渲染页面 - 子组件告诉父组件去更新数据
            this.$emit('delDepts') // 触发自定义事件
          })
      }
    }
  }
}
</script>

<style>
</style>
